package dk.muldbjergskolen.gui;

import dk.muldbjergskolen.gui.*;
import dk.muldbjergskolen.model.Klasse;
import dk.muldbjergskolen.ctr.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.jws.Oneway;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.xml.bind.ParseConversionEvent;


import java.awt.Dialog.ModalityType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.TileObserver;
import java.util.ArrayList;

public class RetKlasse extends JDialog
{
	private JTextField textField_KlasseNavn;
    private JComboBox comboBox_Aargang;
    private JComboBox comboBox_klasser;
    private KlasseCtr kC = new KlasseCtr();
    private JFrame frame;


	/**
	 * Create the dialog.
	 */
	public RetKlasse(RetKlasseListener listener)
	{
		retKlasseListener = listener;
		
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("Ret Klasse");
		setBounds(100, 100, 330, 180);
		getContentPane().setLayout(new BorderLayout());
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						ret();
					}

					
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		{
			JPanel panel = new JPanel();
			getContentPane().add(panel, BorderLayout.CENTER);
			panel.setLayout(null);
			{
				JLabel lblNewLabel = new JLabel("Klassenavn:");
				lblNewLabel.setBounds(10, 45, 92, 14);
				panel.add(lblNewLabel);
			}
			{
				textField_KlasseNavn = new JTextField();
				textField_KlasseNavn.setBounds(112, 42, 132, 20);
				panel.add(textField_KlasseNavn);
				textField_KlasseNavn.setColumns(10);
			}
			{
				JLabel lblForkortelse = new JLabel("Årgang:");
				lblForkortelse.setBounds(10, 76, 92, 14);
				panel.add(lblForkortelse);
			}
			{
				comboBox_Aargang = new JComboBox();
				comboBox_Aargang.setModel(new DefaultComboBoxModel(new String[] {"2012", "2013", "2014", "2015", "2016", "2017", "..."}));
				comboBox_Aargang.setBounds(112, 73, 132, 20);
				panel.add(comboBox_Aargang);
			}
			
			comboBox_klasser = new JComboBox();
			comboBox_klasser.setModel(new DefaultComboBoxModel(new String[] {"dmaa02"}));
			comboBox_klasser.setBounds(112, 11, 132, 20);
			panel.add(comboBox_klasser);
			opdaterKlasser();
			
			JLabel lblVlg = new JLabel("Vælg klasse:");
			lblVlg.setBounds(10, 14, 92, 14);
			panel.add(lblVlg);
		}
	}
	//Opdatere ComboBoxen klasser, så den indeholder de klasser der er i databasen.
	public void opdaterKlasser()
	{
		try
		{
			comboBox_klasser.removeAllItems();
			ArrayList<dk.muldbjergskolen.model.Klasse> a = kC.findKlasser();
		
			for(int i = 0; i < a.size(); i++)
			{
				comboBox_klasser.addItem(a.get(i).getId());
			}
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(this, "Ingen klasser blev fundet.");
		}
	}
	

	//
	// Event håndtering.
	//

	// "Lyttere"
	RetKlasseListener retKlasseListener;

	public interface RetKlasseListener
	{
		void OnRettetKlasse();
	}

	/***
	 * Kaldes når en ny klasse er lavet.
	 */
	private void OnRettetKlasse()
	{
		if (retKlasseListener != null)
			retKlasseListener.OnRettetKlasse();
	}
	
	// retter en klasse
	public void ret()
	{
		try
		{
			int glId = Integer.parseInt(comboBox_klasser.getSelectedItem().toString());
			Klasse k = new Klasse(textField_KlasseNavn.getText(), Integer.parseInt(comboBox_Aargang.getSelectedItem().toString()));
			kC.retKlasse(k, glId);
		}
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(frame, "Udfyld alle Felterne");
			return;
		}
		OnRettetKlasse();
		setVisible(false);
	}
}

  
	      

